const { ruleList, getUser, guanzhu, getPhone, getCode } = require("../../utils/api");
// 获取应用实例
const app = getApp()


Page({
  data: {
    userInfo: {
      userimg: '',
      nickname: '',
      userid: '',
      point: '',
      age: '',
      phone: ''
    },

    shopBtnList: [
      {
        id: 0,
        icon: '../../static/mineimg/daifukuan.png',
        title: '全部订单',
        status: '9',
      },
      {
        id: 1,
        icon: '../../static/mineimg/daifahuo.png',
        title: '待发货',
        status: '0',
      },
      {
        id: 2,
        icon: '../../static/mineimg/daishouhuo.png',
        title: '待收货',
        status: '1',
      },
      {
        id: 3,
        icon: '../../static/mineimg/yiwancheng.png',
        title: '已完成',
        status: '2',

      }
    ],
    mineBtnList: [
      { id: 0, title: '关注公众号', border: 1 },
      { id: 1, title: '收货地址', border: 1 },
      { id: 2, title: '专属客服', border: 1 },
      { id: 3, title: '分享好友', border: 1 },
      { id: 4, title: '协议与规则', border: 1 },
      // { id: 5, title: '注销账号', border: 0 }
    ],
    isJwt: false,  //是否登录 false  没登录  true 登录
    loginInfo: {
      code: '',
      nickname: '',
      avatar: ''
    },
    showRule: false,//规则弹窗 
    ruleMsg: {},
    showLogin: false,
    showAge: false,
    phoneDisabled: false,
    fatherData: {},
    showModal: false,
    showErrModal: false,
    errData: '',
    myData: ''
  },


  onLoad() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 4
      })
    }
  },

  onShow() {
    let that = this
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad()

    this.data.myData = perpage.data.preData

    if (this.data.myData) {
      let resStr = this.data.myData
      let strIndex = resStr.indexOf('goodsurl=') + 9
      let resParams = resStr.substr(strIndex)
      this.getCodeMsg(resParams)

    }

    let jwt = wx.getStorageSync('jwt');

    if (jwt) {
      that.setData({
        phoneDisabled: false,
        isJwt: true,
      })
    }
    else {
      that.setData({
        phoneDisabled: true,
        isJwt: false,
      })
    }

    if (this.data.isJwt) {
      this.getUserInfo()
    }
  },
  //扫码方法
  getScanCode() {
    wx.navigateTo({
      url: '/component/camerapage/index',
    });

  },
  //获取二维码数据
  async getCodeMsg(url) {
    let that = this
    let params = {
      goodsurl: url
    }
    await getCode(params).then((res) => {
      if (res.errcode == 0) {
        that.setData({
          showModal: true,
          fatherData: res.data,
          "fatherData.numRes": res.data.canscore * Number(res.data.multiplier),
          "fatherData.nexttime": (res.data.nexttime).substring(0, 10),
          "fatherData.endtime": (res.data.endtime).substring(0, 10),
          "fatherData.goodsurl": url,

        })

        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })

      } else {
        that.setData({
          showErrModal: true,
          errData: res.errmsg,
        })
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })


      }
    })
  },
  settingAge() {
    this.setData({
      showAge: true
    })

  },

  //获取用户信息
  async getUserInfo() {
    let that = this
    await getUser().then((res) => {

      if (res.errcode == 0) {
        let resData = res.user
        that.setData({
          userInfo: {
            userimg: resData.avatar,
            nickname: resData.nickname,
            userid: resData.id,
            point: resData.score,
            age: resData.age,
            phone: resData.phone
          }
        })
      } else {
        setTimeout(() => {
          wx.showToast({
            title: res.errmsg,
            icon: 'none',
            success: (result) => {
              wx.clearStorage({
                success: function (res) {
                  that.setData({
                    isJwt: false
                  })
                }
              })
            },
          });
        }, 1000);


      }
    })
  },


  onLogin() {
    this.setData({
      showLogin: true
    })
  },

  //关注公众号，收货地址，专属客服，分享好友，协议
  onMineBtn(e) {
    let that = this
    switch (e.currentTarget.dataset.item.id) {
      //关注公众号
      case 0:
        if (!that.data.isJwt) {
          that.setData({
            showLogin: true
          })
        } else {
          wx.navigateTo({
            url: '/packageA/pages/gongzhonghao/index',
            success: (result) => {
              guanzhu().then((res) => {
              })
            },
          });
        }
        break;

      //收货地址
      case 1:
        if (!that.data.isJwt) {
          that.setData({
            showLogin: true
          })
        } else {
          wx.navigateTo({ url: '/packageA/pages/addresspage/index' })
        }
        break;

      //客服
      case 2:
        wx.openCustomerServiceChat({
          extInfo: {
            url: 'https://work.weixin.qq.com/kfid/kfc3a30b968983077ab'
          },
          corpId: 'ww7a86e1a5b34e185b', //企业微信ID
          success(res) {
          },
        })
        break;

      //分享好友
      case 3:
        if (!that.data.isJwt) {
          that.setData({
            showLogin: true
          })
        } else {



        }
        break;

      case 4:
        let params = {
          type: "7"
        }
        ruleList(params).then((res) => {

          if (res.errcode == 0) {
            that.setData({
              showRule: true,
              ruleMsg: res.data
            })

          } else {
          }
        })

        break;

      case 5:

        if (!that.data.isJwt) {
          that.setData({
            phoneDisabled: true,
            showLogin: true

          })
        } else {

          // that.setData({
          //   phoneDisabled: false
          // })
        }
        break;
    }
  },



  //我的背包
  onGoods(e) {
    let that = this
    if (that.data.isJwt) {
      wx.navigateTo({ url: `/packageA/pages/goodslist/index?id=${e.currentTarget.dataset.item.id}&status=${e.currentTarget.dataset.item.status}` })
    } else {
      that.setData({
        showLogin: true
      })
    }
  },

  //积分列表
  onPointDetail() {
    wx.navigateTo({ url: '/packageA/pages/pointpage/index' })
  },

  //退出
  onSignOut() {
    let that = this
    wx.clearStorage({
      success: function (res) {
        that.setData({
          isJwt: false,
          phoneDisabled: true
        })

      }
    })
    wx.showToast({
      title: '已退出登录',
      icon: 'none',
      duration: 2000,

    });

  },

  getPhoneNumber(e) {
    let that = this

    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      let params = {
        code: e.detail.code
      }
      getPhone(params).then((res) => {
        if (res.errcode == 0) {
          wx.showToast({
            title: '手机号绑定完成',
            icon: 'success',

            duration: 1500,
            success: (result) => {
              that.getUserInfo()
              that.setData({
                "userInfo.phone": res.user.phone
              })
            },

          });

        } else {
          wx.showToast({
            title: res.errmsg,
            icon: 'none',

          });

        }
      })
    } else {

    }
  },

  onShareAppMessage(e) {
    const userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    return {
      title: '我在收集积分，快来帮帮我',
      imageUrl: 'https://qmtestimage.oss-cn-guangzhou.aliyuncs.com/katong2.png',
      path: `/pages/homepage/home?shangjiuid=${userInfo.id}`,// 路径，传递参数到指定页面。

    }
  }

})
